﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.DAL;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_Transfers : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_Transfers t = new B_FIM_Transfers();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_HRM_MP_WorkAreas area = new B_HRM_MP_WorkAreas();
        IBLL_FIM_Receives r = new B_FIM_Receives();
        IBLL_FIM_ReceiveDtls rd = new B_FIM_ReceiveDtls();
        IBLL_FIM_MAS_BankAccounts acc = new B_FIM_MAS_BankAccounts();
        IBLL_FIM_MAS_BankAccountTypes acctype = new B_FIM_MAS_BankAccountTypes();
        IBLL_FIM_Cheques cheque = new B_FIM_Cheques();
        IBLL_FIM_PaymentCheques paycheque = new B_FIM_PaymentCheques();
        IBLL_FIM_Payments pay = new B_FIM_Payments();
        IBLL_CodeGenerate cg = new B_CodeGenerate();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int year = main.GetSystemControls().default_bgm_year.Value;
                txtBgmYear.Text = year.ToString();
                txtBgmYear.MinValue = year - 10;
                txtBgmYear.MaxValue = year + 1;
                lblBgmYear.Text = year.ToString();
                lblBgmYear2.Text = year.ToString();
                lblBgmYear3.Text = year.ToString();

                UpdateUI1("ViewAll");
                LoadDDLFilterBgmSourceSeq();
                LoadDDLFilterWorkAreaSeq();
                LoadDDLBankAccSeq();
                LoadRBLBankAccTypeSeq();

                ddlFilterBgmSourceSeq.SelectedIndex = 0;
                string strBgmSourceName = ddlFilterBgmSourceSeq.SelectedItem.Text;
                string strBgmYear = txtBgmYear.Text;
                lblBgmSourceName.Text = strBgmSourceName;
                lblBgmSourceName2.Text = strBgmSourceName;
                lblBgmSourceName3.Text = strBgmSourceName;
            }
        }

        protected void LoadRadGrid1()
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            string strTransferType = rblTransferType.SelectedValue;
            RadGrid1.DataSource = t.GetFull(strBgmSourceSeq, strBgmYear, strWorkAreaSeq, strTransferType);
        }

        protected void LoadRadGrid2(string transfer_seq)
        {
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            RadGrid2.DataSource = r.GetNotDeposits(strBgmSourceSeq, null, strBgmYear, transfer_seq);
            RadGrid2.DataBind();
        }

        protected void CheckDepositedReceives(string transfer_seq)
        {
            for (int i = 0; i < RadGrid2.Items.Count; i++)
            {
                if (RadGrid2.Items[i]["deposit_seq"].Text == transfer_seq)
                {
                    RadGrid2.Items[i].Selected = true;
                }
            }
        }

        protected void LoadDDLFilterBgmSourceSeq()
        {
            ddlFilterBgmSourceSeq.DataSource = source.GetFull(true);
            ddlFilterBgmSourceSeq.DataBind();
        }

        protected void LoadDDLFilterWorkAreaSeq()
        {
            ddlFilterWorkAreaSeq.DataSource = area.GetList();
            ddlFilterWorkAreaSeq.DataBind();

            main.InsertEmptyDDLAll(ddlFilterWorkAreaSeq);
            ddlFilterWorkAreaSeq.SelectedIndex = 0;
        }

        protected void LoadDDLBankAccSeq()
        {
            bool bCrossBudget = PanelEdit3.Visible && rblCrossBudget.SelectedValue == "2";

            var list = acc.GetList(null, ddlFilterBgmSourceSeq.SelectedValue, null, null);
            var list2 = acc.GetList(null, (bCrossBudget ? null : ddlFilterBgmSourceSeq.SelectedValue), null, null);
            ddlToBankAccSeq.DataSource = list;
            ddlFromBankAccSeq2.DataSource = list;
            ddlFromBankAccSeq3.DataSource = list;
            ddlToBankAccSeq3.DataSource = list2;

            ddlToBankAccSeq.DataBind();
            ddlFromBankAccSeq2.DataBind();
            ddlFromBankAccSeq3.DataBind();
            ddlToBankAccSeq3.DataBind();
        }

        protected void LoadRBLBankAccTypeSeq()
        {
            rblBankAccTypeSeq.DataSource = acctype.GetList("3");
            rblBankAccTypeSeq.DataBind();

            if (rblBankAccTypeSeq.Items.Count > 0)
                rblBankAccTypeSeq.SelectedIndex = 0;
        }

        protected void LoadDDLCheque()
        {
            string strBankAccSeq = ddlFromBankAccSeq3.SelectedValue;
            string strPaySeq = lblPaySeq.Text;

            if (string.IsNullOrEmpty(strBankAccSeq))
            {
                return;
            }

            if (string.IsNullOrEmpty(strPaySeq))
            {
                ddlChequeSeq.DataSource = cheque.GetList(strBankAccSeq, null, null, "1");
            }
            else
            {
                ddlChequeSeq.DataSource = cheque.GetList(strBankAccSeq, strPaySeq, null, "1");
            }

            ddlChequeSeq.DataBind();
        }

        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                case "Insert":
                case "Update":
                    cmdViewAll1.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = true;
                    PanelEdit1.Visible = false;
                    PanelEdit2.Visible = false;
                    PanelEdit3.Visible = false;
                    break;
                case "AddNew":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;

                    PanelRadGrid1.Visible = false;
                    switch (rblTransferType.SelectedValue)
                    {
                        case "1":
                            PanelEdit1.Visible = true;
                            PanelEdit2.Visible = false;
                            PanelEdit3.Visible = false;
                            break;
                        case "2":
                            PanelEdit1.Visible = false;
                            PanelEdit2.Visible = true;
                            PanelEdit3.Visible = false;
                            break;
                        case "3":
                            PanelEdit1.Visible = false;
                            PanelEdit2.Visible = false;
                            PanelEdit3.Visible = true;
                            break;
                    }
                    break;
                case "Edit":
                    cmdViewAll1.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;

                    PanelRadGrid1.Visible = false;
                    switch (rblTransferType.SelectedValue)
                    {
                        case "1":
                            PanelEdit1.Visible = true;
                            PanelEdit2.Visible = false;
                            PanelEdit3.Visible = false;
                            break;
                        case "2":
                            PanelEdit1.Visible = false;
                            PanelEdit2.Visible = true;
                            PanelEdit3.Visible = false;
                            break;
                        case "3":
                            PanelEdit1.Visible = false;
                            PanelEdit2.Visible = false;
                            PanelEdit3.Visible = true;
                            break;
                    }
                    break;
            }
        }

        protected void ClearControl1()
        {
            lblPaySeq.Text = "";
            lblTransferSeq.Text = "";
            lblRefNo.Text = "";

        }

        //============================ control events ==============================

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            //if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            //{
            //    e.Item.Selected = true;
            //    string strReceiveSeq = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["receive_seq"].Text;
            //    Session["ReceiveSeq"] = strReceiveSeq;
            //    LoadRadGrid2(strReceiveSeq);
            //}
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                LoadRadGrid1();
            }
        }

        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Sort" || e.CommandName == "Filter")
            {
                if (RadGrid1.SelectedItems.Count > 0)
                {
                    string strTransferSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["transfer_seq"].Text;
                    LoadRadGrid2(main.WebString(strTransferSeq));
                }
                else
                {
                    LoadRadGrid2(null);
                }
            }
            else if (e.CommandName == "RowClick")
            {
                if (e.Item.OwnerTableView.ParentItem == null)
                {
                    double dSum = 0;
                    for (int i = 0; i < RadGrid2.Items.Count; i++)
                    {
                        if (RadGrid2.Items[i].Selected)
                        {
                            dSum = dSum + Convert.ToDouble(main.WebString(RadGrid2.Items[i]["amount"].Text));
                        }
                    }

                    lblAmount.Text = dSum.ToString("#,##0.00");
                }
            }
        }

        //protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        //{
        //    if (e.Item is GridDataItem)
        //    {
        //        GridDataItem dataItem = (GridDataItem)e.Item;
        //        if (e.Item.DataItem.ToString() == "KPIS.GERP.GFMS.MODEL.M_FIM_ReceiveDtls")
        //        {
        //            RadNumericTextBox txtAmount = (RadNumericTextBox)dataItem["amount"].FindControl("txtAmount");
        //            RadNumericTextBox txtExchangeRate = (RadNumericTextBox)dataItem["exchange_rate"].FindControl("txtExchangeRate");
        //            RadNumericTextBox txtBathAmount = (RadNumericTextBox)dataItem["bath_amount"].FindControl("txtBathAmount");

        //            txtExchangeRate.Attributes.Add("OnBlur", string.Format("txtBlur('{0}', '{1}', '{2}')"
        //                , txtAmount.ClientID, txtExchangeRate.ClientID, txtBathAmount.ClientID));
        //        }
        //    }
        //}

        protected void ddlFilterBgmSourceSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
            LoadDDLBankAccSeq();

            string strBgmSourceName = ddlFilterBgmSourceSeq.SelectedItem.Text;
            lblBgmSourceName.Text = strBgmSourceName;
            lblBgmSourceName2.Text = strBgmSourceName;
            lblBgmSourceName3.Text = strBgmSourceName;
        }

        protected void ddlFilterWorkAreaSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void txtBgmYear_TextChanged(object sender, EventArgs e)
        {
            LoadRadGrid1();
            RadGrid1.DataBind();

            string strBgmYear = txtBgmYear.Text;
            lblBgmYear.Text = strBgmYear;
            lblBgmYear2.Text = strBgmYear;
            lblBgmYear3.Text = strBgmYear;
        }

        protected void cmdViewAll1_Click(object sender, EventArgs e)
        {
            UpdateUI1("ViewAll");
            ClearControl1();
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");

            switch (rblTransferType.SelectedValue)
            {
                case "1":
                    LoadRadGrid2(null);
                    dpTransferDate.SetValue(DateTime.Today);
                    lblEmpSeq.Text = Session["EmpSeq"].ToString();
                    lblEmpName.Text = Session["UserName"].ToString();
                    lblAmount.Text = "0";
                    break;
                case "2":
                    dpTransferDate2.SetValue(DateTime.Today);
                    lblEmpSeq2.Text = Session["EmpSeq"].ToString();
                    lblEmpName2.Text = Session["UserName"].ToString();
                    break;
                case "3":
                    dpTransferDate3.SetValue(DateTime.Today);
                    lblEmpSeq3.Text = Session["EmpSeq"].ToString();
                    lblEmpName3.Text = Session["UserName"].ToString();
                    break;
            }

            ClearControl1();
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                string strTransferSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["transfer_seq"].Text;
                //string strTransferType = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["transfer_type"].Text;
                string strTransferName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["transfer_name"].Text;
                string strRefNoOutside = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["ref_no_outside"].Text;
                string strTransferDate = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["transfer_date"].Text;
                string strFromBankAccSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["from_bank_acc_seq"].Text;
                string strToBankAccSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["to_bank_acc_seq"].Text;
                string strBankAccTypeSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bank_acc_type_seq"].Text;
                string strAmount = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["amount"].Text;
                //string strCurrencySeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["currency_seq"].Text;
                //string strExchangeRate = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["exchange_rate"].Text;
                //string strBathAmount = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bath_amount"].Text;
                string strEmpSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["emp_seq"].Text;
                string strEmpText = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["emp_text"].Text;
                string strChequeSeq = main.WebString(RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["cheque_seq"].Text);
                string strPaySeq = main.WebString(RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["pay_seq"].Text);

                switch (rblTransferType.SelectedValue)
                {
                    case "1":
                        LoadRadGrid2(strTransferSeq);
                        CheckDepositedReceives(strTransferSeq);

                        double dSum = 0;
                        for (int i = 0; i < RadGrid2.Items.Count; i++)
                        {
                            if (RadGrid2.Items[i].Selected)
                            {
                                dSum = dSum + Convert.ToDouble(main.WebString(RadGrid2.Items[i]["amount"].Text));
                            }
                        }

                        strAmount = (main.WebString(strAmount).Length > 0 ? main.WebString(strAmount) : "0");
                        lblAmount.Text = dSum.ToString();
                        txtOtherAmount.Text = (Convert.ToDouble(strAmount) - dSum).ToString();

                        lblTransferSeq.Text = main.WebString(strTransferSeq);
                        txtTransferName.Text = main.WebString(strTransferName);
                        txtRefNoOutside.Text = main.WebString(strRefNoOutside);
                        dpTransferDate.SetValue(main.WebString(strTransferDate));
                        ddlToBankAccSeq.SelectedValue = main.WebString(strToBankAccSeq);
                        ddlToBankAccSeq_SelectedIndexChanged(null, null);

                        if (rblBankAccTypeSeq.Visible)
                        {
                            rblBankAccTypeSeq.SelectedValue = main.WebString(strBankAccTypeSeq);
                        }

                        lblEmpSeq.Text = main.WebString(strEmpSeq);
                        lblEmpName.Text = main.WebString(strEmpText);

                        //strBathAmount = (main.WebString(strBathAmount).Length > 0 ? main.WebString(strBathAmount) : "0");
                        //lblBathAmount.Text = strBathAmount;
                        break;
                    case "2":
                        lblTransferSeq2.Text = main.WebString(strTransferSeq);
                        txtTransferName2.Text = main.WebString(strTransferName);
                        txtRefNoOutside2.Text = main.WebString(strRefNoOutside);
                        dpTransferDate2.SetValue(main.WebString(strTransferDate));
                        ddlFromBankAccSeq2.SelectedValue = main.WebString(strFromBankAccSeq);
                        lblEmpSeq2.Text = main.WebString(strEmpSeq);
                        lblEmpName2.Text = main.WebString(strEmpText);
                        txtAmount2.Text = main.WebString(strAmount);
                        break;
                    case "3":
                        lblTransferSeq3.Text = main.WebString(strTransferSeq);
                        txtTransferName3.Text = main.WebString(strTransferName);
                        txtRefNoOutside3.Text = main.WebString(strRefNoOutside);
                        dpTransferDate3.SetValue(main.WebString(strTransferDate));
                        ddlFromBankAccSeq3.SelectedValue = main.WebString(strFromBankAccSeq);
                        ddlToBankAccSeq3.SelectedValue = main.WebString(strToBankAccSeq);
                        lblEmpSeq3.Text = main.WebString(strEmpSeq);
                        lblEmpName3.Text = main.WebString(strEmpText);
                        txtAmount3.Text = main.WebString(strAmount);

                        if (!string.IsNullOrEmpty(strChequeSeq))
                        {
                            rblTransferMethod.SelectedValue = "2";
                            rblTransferMethod_SelectedIndexChanged(null, null);
                            ddlChequeSeq.SelectedValue = strChequeSeq;
                            lblPaySeq.Text = strPaySeq;
                        }
                        else
                        {
                            rblTransferMethod.SelectedValue = "1";
                            rblTransferMethod_SelectedIndexChanged(null, null);
                        }

                        break;
                }                
            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการแก้ไข");
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                M_FIM_Transfers info = new M_FIM_Transfers();
                string strUserID = Session["EmpSeq"].ToString();
                DateTime dtNow = DateTime.Now;
                string strTransferSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["transfer_seq"].Text;

                info.transfer_seq = Convert.ToInt32(strTransferSeq);

                info.updated_by = main.ConvertNullInt(strUserID);
                info.updated_when = main.ConvertNullDateTime(dtNow);

                t.Delete(info);

                LoadRadGrid1();
                RadGrid1.DataBind();
            }
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            M_FIM_Transfers info = new M_FIM_Transfers();
            M_FIM_MAS_BankAccounts infoBankAcc = new M_FIM_MAS_BankAccounts();
            string strTransferSeq = null;
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strTransferType = rblTransferType.SelectedValue;
            string strTransferName = null;
            string strRefNo = "";
            string strRefNoOutside = "";
            string strTransferDate = null;
            string strFromBankAccSeq = null;
            string strToBankAccSeq = null;
            string strBankAccTypeSeq = null;
            string strAmount = null;
            string strEmpSeq = null;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;
            string strReceiveSeq;
            string strChequeSeq = ddlChequeSeq.SelectedValue;

            switch (rblTransferType.SelectedValue)
            {
                case "1":
                    strTransferSeq = lblTransferSeq.Text;
                    strTransferName = txtTransferName.Text;
                    strRefNoOutside = txtRefNoOutside.Text;
                    strTransferDate = dpTransferDate.GetStringValue();
                    strToBankAccSeq = ddlToBankAccSeq.SelectedValue;

                    if (rblBankAccTypeSeq.Visible)
                        strBankAccTypeSeq = rblBankAccTypeSeq.SelectedValue;

                    strAmount = (lblAmount.Text.Length > 0 ? lblAmount.Text : "0");
                    string strOtherAmount = (txtOtherAmount.Text.Length > 0 ? txtOtherAmount.Text : "0");

                    strAmount = (main.ConvertNullDouble(strAmount) + main.ConvertNullDouble(strOtherAmount)).ToString();
                    strEmpSeq = lblEmpSeq.Text;

                    infoBankAcc = acc.GetInfo(strToBankAccSeq);
                    strRefNo = cg.GetNextCode(TransectionType.FimTransfersRefNo, (infoBankAcc.bgm_source_seq ?? 1) == 1 ? "1" : "2");
                    break;
                case "2":
                    strTransferSeq = lblTransferSeq2.Text;
                    strTransferName = txtTransferName2.Text;
                    strRefNoOutside = txtRefNoOutside2.Text;
                    strTransferDate = dpTransferDate2.GetStringValue();
                    strFromBankAccSeq = ddlFromBankAccSeq2.SelectedValue;
                    strAmount = txtAmount2.Text;
                    strEmpSeq = lblEmpSeq2.Text;

                    infoBankAcc = acc.GetInfo(strFromBankAccSeq);
                    strRefNo = cg.GetNextCode(TransectionType.FimTransfersRefNo, (infoBankAcc.bgm_source_seq ?? 1) == 1 ? "1" : "2");
                    break;
                case "3":
                    strTransferSeq = lblTransferSeq3.Text;
                    strTransferName = txtTransferName3.Text;
                    strRefNoOutside = txtRefNoOutside3.Text;
                    strTransferDate = dpTransferDate3.GetStringValue();
                    strFromBankAccSeq = ddlFromBankAccSeq3.SelectedValue;
                    strToBankAccSeq = ddlToBankAccSeq3.SelectedValue;
                    strAmount = txtAmount3.Text;
                    strEmpSeq = lblEmpSeq3.Text;
                    
                    if (rblTransferMethod.SelectedValue == "2")
                    {
                        info.cheque_seq = main.ConvertNullInt(strChequeSeq);
                    }

                    infoBankAcc = acc.GetInfo(strFromBankAccSeq);
                    strRefNo = cg.GetNextCode(TransectionType.FimTransfersRefNo, (infoBankAcc.bgm_source_seq ?? 1) == 1 ? "1" : "2");
                    break;
            }

            //info.transfer_seq = Convert.ToInt32(strTransferSeq);
            info.bgm_source_seq = Convert.ToInt32(strBgmSourceSeq);
            info.work_area_seq = 0;//Convert.ToInt32(strWorkAreaSeq);
            info.bgm_year = Convert.ToInt32(strBgmYear);
            info.transfer_type = strTransferType;
            info.transfer_name = strTransferName;
            info.ref_no = strRefNo;
            info.ref_no_outside = strRefNoOutside;
            info.transfer_date = main.ConvertNullDateTime(strTransferDate);
            info.from_bank_acc_seq = main.ConvertNullInt(strFromBankAccSeq);
            info.to_bank_acc_seq = main.ConvertNullInt(strToBankAccSeq);
            info.bank_acc_type_seq = main.ConvertNullInt(strBankAccTypeSeq);
            info.amount = main.ConvertNullDouble(strAmount);
            info.emp_seq = main.ConvertNullInt(strEmpSeq);

            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = dtNow;

            strTransferSeq = t.Insert(info);

            if (rblTransferType.SelectedValue == "1")
            {
                //t.SelectRecentInsertedRoleSeq();

                for (int i = 0; i < RadGrid2.Items.Count; i++)
                {
                    if (RadGrid2.Items[i].Selected)
                    {
                        M_FIM_Receives receiveInfo = new M_FIM_Receives();
                        strReceiveSeq = RadGrid2.Items[i]["receive_seq"].Text;

                        receiveInfo.receive_seq = Convert.ToInt32(strReceiveSeq);
                        receiveInfo.deposit_seq = main.ConvertNullInt(strTransferSeq);
                        receiveInfo.updated_by = main.ConvertNullInt(strUserID);
                        receiveInfo.updated_when = dtNow;

                        r.UpdateDepositSeq(receiveInfo);
                    }
                }
            }
            else if (rblTransferType.SelectedValue == "3" && rblTransferMethod.SelectedValue == "2")
            {
                var infoPay = new M_FIM_Payments()
                {
                    bgm_source_seq = Convert.ToInt32(strBgmSourceSeq),
                    bgm_year = Convert.ToInt32(strBgmYear),
                    event_seq = 19,
                    pay_date = main.ConvertNullDateTime(strTransferDate),
                    ref_no = cg.GetNextCode(TransectionType.FimPaymentsRefNo, strBgmSourceSeq == "1" ? "1" : "2"),
                    pay_name = "โอนเงินรหัส " + strRefNo,
                    method_seq = 2,
                    contact_seq = 2214,// hard code
                    amount = main.ConvertNullDouble(strAmount),
                    record_status = "W",
                    created_by = main.ConvertNullInt(strUserID),
                    created_when = dtNow,
                };

                string strPaySeq = pay.Insert(infoPay);

                info = t.GetInfo(strTransferSeq);
                info.pay_seq = Convert.ToInt32(strPaySeq);
                t.Update(info);

                var infoPayCheque = new M_FIM_PaymentCheques()
                {
                    pay_seq = main.ConvertNullInt(strPaySeq),
                    cheque_seq = main.ConvertNullInt(strChequeSeq),
                    amount = main.ConvertNullDouble(strAmount),
                    record_status = "U",
                    created_by = main.ConvertNullInt(strUserID),
                    created_when = dtNow,
                };

                paycheque.Insert(infoPayCheque);
            }

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Insert");
            ClearControl1();
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            M_FIM_Transfers info = new M_FIM_Transfers();
            string strTransferSeq = null;
            string strBgmSourceSeq = ddlFilterBgmSourceSeq.SelectedValue;
            string strWorkAreaSeq = ddlFilterWorkAreaSeq.SelectedValue;
            string strBgmYear = txtBgmYear.Text;
            string strTransferType = rblTransferType.SelectedValue;
            string strTransferName = null;
            string strRefNo = lblRefNo.Text;
            string strRefNoOutside = null;
            string strTransferDate = null;
            string strFromBankAccSeq = null;
            string strToBankAccSeq = null;
            string strBankAccTypeSeq = null;
            string strAmount = null;
            string strEmpSeq = null;
            string strUserID = Session["EmpSeq"].ToString();
            DateTime dtNow = DateTime.Now;
            string strReceiveSeq;
            string strChequeSeq = ddlChequeSeq.SelectedValue;
            string strPaySeq = lblPaySeq.Text;

            M_FIM_Transfers currentInfo = null;
            switch (rblTransferType.SelectedValue)
            {
                case "1":
                    strTransferSeq = lblTransferSeq.Text;
                    if((currentInfo = t.GetInfo(strTransferSeq)) == null)
                    {
                        main.MessageAlert(Page, "ไม่พบข้อมูลนี้");
                        return;
                    }
                    strRefNo = currentInfo.ref_no;
                    strTransferName = txtTransferName.Text;
                    strRefNoOutside = txtRefNoOutside.Text;
                    strTransferDate = dpTransferDate.GetStringValue();
                    strToBankAccSeq = ddlToBankAccSeq.SelectedValue;

                    if (rblBankAccTypeSeq.Visible)
                        strBankAccTypeSeq = rblBankAccTypeSeq.SelectedValue;

                    strAmount = lblAmount.Text;
                    string strOtherAmount = (txtOtherAmount.Text.Length > 0 ? txtOtherAmount.Text : "0");

                    strAmount = (main.ConvertNullDouble(strAmount) + main.ConvertNullDouble(strOtherAmount)).ToString();
                    strEmpSeq = lblEmpSeq.Text;
                    break;
                case "2":
                    strTransferSeq = lblTransferSeq2.Text;
                    if((currentInfo = t.GetInfo(strTransferSeq)) == null)
                    {
                        main.MessageAlert(Page, "ไม่พบข้อมูลนี้");
                        return;
                    }
                    strRefNo = currentInfo.ref_no;
                    strTransferName = txtTransferName2.Text;
                    strRefNoOutside = txtRefNoOutside2.Text;
                    strTransferDate = dpTransferDate2.GetStringValue();
                    strFromBankAccSeq = ddlFromBankAccSeq2.SelectedValue;
                    strAmount = txtAmount2.Text;
                    strEmpSeq = lblEmpSeq2.Text;
                    break;
                case "3":
                    strTransferSeq = lblTransferSeq3.Text;
                    if((currentInfo = t.GetInfo(strTransferSeq)) == null)
                    {
                        main.MessageAlert(Page, "ไม่พบข้อมูลนี้");
                        return;
                    }
                    strRefNo = currentInfo.ref_no;
                    strTransferName = txtTransferName3.Text;
                    strRefNoOutside = txtRefNoOutside3.Text;
                    strTransferDate = dpTransferDate3.GetStringValue();
                    strFromBankAccSeq = ddlFromBankAccSeq3.SelectedValue;
                    strToBankAccSeq = ddlToBankAccSeq3.SelectedValue;
                    strAmount = txtAmount3.Text;
                    strEmpSeq = lblEmpSeq3.Text;

                    if (rblTransferMethod.SelectedValue == "2")
                    {
                        info.cheque_seq = main.ConvertNullInt(strChequeSeq);
                    }
                    break;
            }

            info.transfer_seq = Convert.ToInt32(strTransferSeq);
            info.bgm_source_seq = Convert.ToInt32(strBgmSourceSeq);
            info.work_area_seq = 0;// Convert.ToInt32(strWorkAreaSeq);
            info.bgm_year = Convert.ToInt32(strBgmYear);
            info.transfer_type = strTransferType;
            info.transfer_name = strTransferName;
            info.ref_no = strRefNo;
            info.ref_no_outside = strRefNoOutside;
            info.transfer_date = main.ConvertNullDateTime(strTransferDate);
            info.from_bank_acc_seq = main.ConvertNullInt(strFromBankAccSeq);
            info.to_bank_acc_seq = main.ConvertNullInt(strToBankAccSeq);
            info.bank_acc_type_seq = main.ConvertNullInt(strBankAccTypeSeq);
            info.amount = main.ConvertNullDouble(strAmount);
            info.emp_seq = main.ConvertNullInt(strEmpSeq);

            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = main.ConvertNullDateTime(dtNow);

            t.Update(info);

            if (rblTransferType.SelectedValue == "1")
            {
                for (int i = 0; i < RadGrid2.Items.Count; i++)
                {
                    M_FIM_Receives receiveInfo = new M_FIM_Receives();
                    strReceiveSeq = RadGrid2.Items[i]["receive_seq"].Text;

                    receiveInfo.receive_seq = Convert.ToInt32(strReceiveSeq);
                    receiveInfo.updated_by = main.ConvertNullInt(strUserID);
                    receiveInfo.updated_when = main.ConvertNullDateTime(dtNow);

                    if (RadGrid2.Items[i].Selected)
                    {
                        receiveInfo.deposit_seq = main.ConvertNullInt(strTransferSeq);

                        r.UpdateDepositSeq(receiveInfo);
                    }
                    else
                    {
                        receiveInfo.deposit_seq = null;

                        r.UpdateDepositSeq(receiveInfo);
                    }
                }
            }
            else if (rblTransferType.SelectedValue == "3" && rblTransferMethod.SelectedValue == "2")
            {
                var infoPay = new M_FIM_Payments()
                {
                    bgm_source_seq = Convert.ToInt32(strBgmSourceSeq),
                    bgm_year = Convert.ToInt32(strBgmYear),
                    event_seq = 19,
                    pay_date = main.ConvertNullDateTime(strTransferDate),
                    ref_no = cg.GetNextCode(TransectionType.FimPaymentsRefNo, strBgmSourceSeq == "1" ? "1" : "2"),
                    pay_name = "โอนเงินรหัส " + strRefNo,
                    method_seq = 2,
                    contact_seq = 2214,// hard code
                    amount = main.ConvertNullDouble(strAmount),
                    
                    created_by = main.ConvertNullInt(strUserID),
                    created_when = dtNow,
                    updated_by = main.ConvertNullInt(strUserID),
                    updated_when = dtNow,
                };

                if (string.IsNullOrEmpty(strPaySeq))
                {
                    infoPay.record_status = "W";
                    strPaySeq = pay.Insert(infoPay);

                    info = t.GetInfo(strTransferSeq);
                    info.pay_seq = Convert.ToInt32(strPaySeq);
                    t.Update(info);

                    var infoPayCheque = new M_FIM_PaymentCheques()
                    {
                        pay_seq = main.ConvertNullInt(strPaySeq),
                        cheque_seq = main.ConvertNullInt(strChequeSeq),
                        amount = main.ConvertNullDouble(strAmount),
                        record_status = "U",
                        created_by = main.ConvertNullInt(strUserID),
                        created_when = dtNow,
                    };

                    paycheque.Insert(infoPayCheque);
                }
                else
                {
                    infoPay.pay_seq = Convert.ToInt32(strPaySeq);
                    pay.Update(infoPay);

                    var infoPayCheque = paycheque.GetFull(strPaySeq, null).FirstOrDefault();
                    infoPayCheque.amount = main.ConvertNullDouble(strAmount);
                    infoPayCheque.updated_by = main.ConvertNullInt(strUserID);
                    infoPayCheque.updated_when = dtNow;
                    paycheque.Update(infoPayCheque);
                }
            }

            LoadRadGrid1();
            RadGrid1.DataBind();
            UpdateUI1("Update");
            ClearControl1();
        }

        protected void rblTransferType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (rblTransferType.SelectedValue)
            {
                case "1":
                    lblHeader.Text = "ข้อมูลรายการฝากเงิน";
                    RadGrid1.Columns[15].HeaderText = "เลขที่ใบนำฝาก";
                    RadGrid1.Columns[17].Visible = false;
                    RadGrid1.Columns[18].Visible = true;
                    break;
                case "2":
                    lblHeader.Text = "ข้อมูลรายการถอนเงิน";
                    RadGrid1.Columns[15].HeaderText = "เลขที่ใบถอน";
                    RadGrid1.Columns[17].Visible = true;
                    RadGrid1.Columns[18].Visible = false;
                    break;
                case "3":
                    lblHeader.Text = "ข้อมูลรายการโอนเงิน";
                    RadGrid1.Columns[15].HeaderText = "เลขที่ใบโอน";
                    RadGrid1.Columns[17].Visible = true;
                    RadGrid1.Columns[18].Visible = true;
                    break;
            }
            LoadRadGrid1();
            RadGrid1.DataBind();
        }

        protected void ddlToBankAccSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string strBankAccTypeGrpSeq = acctype.GetInfo(ddlToBankAccSeq.SelectedValue).bank_acc_type_grp_seq.ToString();

            PanelBankAccType.Visible = strBankAccTypeGrpSeq == "3";
        }

        protected void rblTransferMethod_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblTransferMethod.SelectedValue == "1")
            {
                tableCheque.Visible = false;
                tableCrossBudget.Visible = true;
                //LoadDDLCheque();
            }
            else if (rblTransferMethod.SelectedValue == "2")
            {
                tableCheque.Visible = true;
                tableCrossBudget.Visible = true;
                LoadDDLCheque();
            }
            else
            {
                tableCheque.Visible = true;
                tableCrossBudget.Visible = true;
                LoadDDLCheque();
            }
        }

        protected void ddlFromBankAccSeq3_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (rblTransferMethod.SelectedValue == "2")
            {
                LoadDDLCheque();
            }
        }

        protected void rblCrossBudget_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDDLBankAccSeq();
        }

        //==========================================================================
    }
}
